package com.sms.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sms.dao.IDataDictDAO;
import com.sms.po.TDatadict;

public class DataDictDAO extends HibernateDaoSupport implements IDataDictDAO {
	private static final Log log = LogFactory.getLog(DataDictDAO.class);
	@Override
	public boolean addDataDict(TDatadict tdt) {
		log.debug("add DataDict instance...");
		try{
			this.getHibernateTemplate().save(tdt);
			return true;
		}catch(RuntimeException e){
			log.error("add DataDict instance failed:",e);
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public int getDataDictCount(final String dataKey, final String flag, final String value,final String remark) {
		log.debug("find DataDict by datakey:"+dataKey + " flag:"+flag + " value:"+ value + " count ");
		try{
			return Integer.valueOf(this.getHibernateTemplate().execute(new HibernateCallback(){
				@SuppressWarnings("unchecked")
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					List<Object> l = null;
					try {
						String hql = "select count(*) from TDatadict where 1=1 ";
						if(dataKey != null && dataKey.trim().length() > 0)
							hql += " and datakey = :datakey ";
						if(flag != null && flag.trim().length() > 0)
							hql += " and flag = :flag ";
						if(value != null && value.trim().length() > 0)
							hql += " and value = :value ";
						if(remark != null && remark.trim().length() > 0)
							hql += " and remark like :remark ";
						Query q =  session.createQuery(hql);
						if(dataKey != null && dataKey.trim().length() > 0)
							q.setString("datakey", dataKey);
						if(flag != null && flag.trim().length() > 0)
							q.setString("flag", flag);
						if(value != null && value.trim().length() > 0)
							q.setString("value", value);
						if(remark != null && remark.trim().length() > 0)
							q.setString("remark", "%"+remark+"%");
						l = q.list();
					} catch (Exception e) {
						e.printStackTrace();
					}finally{
						session.close();
					}
					return l.get(0).toString();
				}
				
			}).toString() ).intValue();
		}catch(RuntimeException re){
			log.error("find DataDict by datakey:"+dataKey + " flag:"+flag + " value:"+ value + " count  fialed");
			throw re;
		}
	}

	@Override
	public boolean removeDataDict(TDatadict tdt) {
		log.debug("remove DataDict instance...");
		try{
			this.getHibernateTemplate().delete(tdt);
			return true;
		}catch(RuntimeException e){
			log.error("remove DataDict instance failed:",e);
			e.printStackTrace();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TDatadict> showDataDictByPage(final String dataKey,final String flag,
			final String value,final String remark, final int start,final  int limit) {
		log.debug("find DataDict by datakey:"+dataKey + " flag:"+flag + " value:"+ value + " count ");
		try{
			return (List<TDatadict>) this.getHibernateTemplate().execute(new HibernateCallback(){
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					List<TDatadict> l = null;
					try {
						String hql = "from TDatadict where 1=1 ";
						if (dataKey != null && dataKey.trim().length() > 0)
							hql += " and datakey = :datakey ";
						if (flag != null && flag.trim().length() > 0)
							hql += " and flag = :flag ";
						if (value != null && value.trim().length() > 0)
							hql += " and value = :value ";
						if (remark != null && remark.trim().length() > 0)
							hql += " and remark like :remark ";
						Query q = session.createQuery(hql);
						if (dataKey != null && dataKey.trim().length() > 0)
							q.setString("datakey", dataKey);
						if (flag != null && flag.trim().length() > 0)
							q.setString("flag", flag);
						if (value != null && value.trim().length() > 0)
							q.setString("value", value);
						if (remark != null && remark.trim().length() > 0)
							q.setString("remark", "%" + remark + "%");
						q.setFirstResult(start);
						q.setMaxResults(limit);
						l = q.list();
					} catch (Exception e) {
						e.printStackTrace();
					}finally{
						session.close();
					}
					return l;
				}
				
			}) ;
		}catch(RuntimeException re){
			log.error("find DataDict by datakey:"+dataKey + " flag:"+flag + " value:"+ value + " count  fialed");
			re.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean updateDataDict(TDatadict tdt) {
		log.debug("update DataDict instance...");
		try{
			this.getHibernateTemplate().update(tdt);
			return true;
		}catch(RuntimeException e){
			log.error("update DataDict instance failed:",e);
			e.printStackTrace();
		}
		return false;
	}
	
	

}
